package cn.iwit.srv;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import cn.iwit.util.BusiException;
import cn.iwit.util.SqlUtil;


@Component
@Transactional(readOnly = true)
public class BaseService {
	private Logger logger = LoggerFactory.getLogger(BaseService.class);
	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	
	/**
	 * 获取数据库记录集
	 * 
	 * @param sql sql语句
	 * @param args 参数
	 * @param pagesize 每页记录数
	 * @param page 当前页码
	 * @return
	 */
	public List<Map<String, Object>> query(String sql, Object[] args, int pagesize, int page) throws BusiException {
		logger.info("执行查询语句{},pagesize={},page={}", sql, pagesize, page);
		if (pagesize > 1000)
			throw new BusiException("600002", "每次查询最大返回记录数不能超过1000条");
		String sql1 = SqlUtil.getLimitRecord(sql, page, pagesize);
		try {
			long l1 = System.currentTimeMillis();
			List<Map<String, Object>> list = jdbcTemplate.queryForList(sql1, args);
			l1 = System.currentTimeMillis() - l1;
			if (l1 > 1000L) {
				logger.warn("查询数据库性能异常,耗费时间={},sql={},参数={}", l1, sql, getArgsString(args));
			}
			return list;
		} catch (DataAccessException de) {
			logger.warn("查询数据异常,sql={},参数={}", sql, getArgsString(args));
			logger.warn("查询数据异常", de);
			throw new BusiException(BusiException.ERR_DBERROR, "查询数据异常", de);
		}
	}
	/**
	 * 查询结果集，默认最大返回1000条记录
	 * 
	 * @param sql
	 * @param args
	 * @return
	 **/
	public List<Map<String, Object>> query(String sql, Object[] args) throws BusiException {
		return query(sql, args, 1000, 1);
	}
	
	public <T> List<T> queryForList(String sql, Object[] args,Class<T> t){
		logger.info("sql:{},aggs:{}",sql,getArgsString(args));
		return jdbcTemplate.query(sql, args, new BeanPropertyRowMapper<T>(t) );
	}
	public <T> T queryForObject(String sql, Object[] args,Class<T> t){
		logger.info("sql:{},aggs:{}",sql,getArgsString(args));
		return jdbcTemplate.queryForObject(sql, args, new BeanPropertyRowMapper<T>(t));
	}
	public int queryForInt(String sql, Object[] args){
		logger.info("sql:{},aggs:{}",sql,getArgsString(args));
		return jdbcTemplate.queryForObject(sql, args, Integer.class);
	}
	
	public int execUpdate(String sql,Object[] args){
		logger.info("sql:{},aggs:{}",sql,getArgsString(args));
		return jdbcTemplate.update(sql, args);
	}
	
	public int[] execBacthUpdate(String sql,List<Object[]> args){
		return jdbcTemplate.batchUpdate(sql, args);
	}

	
	private String getArgsString(Object[] args) {
		StringBuffer sb = new StringBuffer("args=[");
		if (args != null && args.length > 0) {
			for (int i = 0; i < args.length; i++) {
				sb.append(args[i] + ",");
			}
		}

		String s = sb.toString();
		if (s.endsWith(","))
			s = s.substring(0, s.length() - 1);
		return s + "]";

	}
	
}

